/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

package com.facebook.animated.giflite.draw;
import com.oszc.bbhmlibrary.utils.CommonUtils;
import com.oszc.bbhmlibrary.utils.LogUtil;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.RectFloat;
import ohos.media.image.ImagePacker;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;
import pl.droidsonroids.gif.GifInfoHandle;

import java.io.FileOutputStream;

/**
 * Pronounced Draw-er Draws frames of a Movie to a bitmap. All methods are synchronized, so
 * can be used in parallel. The underlying {@link #mMovie} is not threadsafe, and should therefore
 * not be accessed outside of {@link MovieDrawer}. Attempts to optimize work done by the drawing
 * {@link Canvas} by detecting if the underlying {@link PixelMap} has changed.
 */
public class MovieDrawer {

    private final String TAG = MovieDrawer.class.getSimpleName();
  private final GifInfoHandle mMovie;
  private final MovieScaleHolder mScaleHolder;
  private final Canvas mCanvas;
  private Paint paint;
  private RectFloat mPosition = new RectFloat();

  private PixelMap mPreviousBitmap;

  public MovieDrawer(GifInfoHandle movie) {
    mMovie = movie;
    mScaleHolder = new MovieScaleHolder(movie.getWidth(), movie.getHeight());
    mCanvas = new Canvas();
    paint = new Paint();
    paint.setAntiAlias(true);
  }

  public synchronized void drawFrame(int movieTime, int w, int h, PixelMap bitmap) {

    if (mPreviousBitmap != null && mPreviousBitmap.isReleased()) {
      mPreviousBitmap = null;
    }
    if (mPreviousBitmap != bitmap) {
      mPreviousBitmap = bitmap;
    }
      // /data/user/0/com.facebook.fresco.samples.showcase
    /*
    mScaleHolder.updateViewPort(w,h);
    LogUtil.error(TAG, "canvas drawFrame editable:" + mPreviousBitmap.isEditable()
            + "  previewBitmap:" + mPreviousBitmap
            + "    w:" + w + "   h:" + h
            + "   movie.width:" + mMovie.getWidth() + "   movie.height:" + mMovie.getHeight()
            + "   scaleHolderScale:" + mScaleHolder.getScale()
            + " scaleHolderLeft:" + mScaleHolder.getLeft() + "   scaleHolderTop:" + mScaleHolder.getTop()
            +"  imageFormat:"+ bitmap.getImageInfo().pixelFormat.name()
    );
     */
  //  int frame = movieTime/30;
    mMovie.seekToTime(movieTime,mPreviousBitmap);
   // mMovie.seekToFrame(frame, mPreviousBitmap);



    /*
    mScaleHolder.updateViewPort(w, h);

    mCanvas.save();
    mCanvas.scale(mScaleHolder.getScale(), mScaleHolder.getScale());

    //mPosition.left = mScaleHolder.getLeft();
    //mPosition.top = mScaleHolder.getTop();

    mPosition.left = 0;
    mPosition.top = 0;
    mPosition.right = w;
    mPosition.bottom = h;

    mCanvas.drawPixelMapHolderRect(new PixelMapHolder(mPreviousBitmap),mPosition,paint);
    mCanvas.restore();
     */
  }
}
